﻿#ifndef PST_LINECHARTSTRAIGHTLINE_H
#define PST_LINECHARTSTRAIGHTLINE_H

#include "qcustomplot.h"

#include "LineChartTracer.h"

namespace pst
{

    class LineChartStraightLine : public QCPItemStraightLine
    {
        Q_OBJECT
    public:
        enum Type {
            XAxis,
            YAxis
        };

        struct IntersectionDataLabel
        {
            QCPItemText* label;
            QCPItemLine* arrow;
        };

        explicit LineChartStraightLine(QCustomPlot* parentPlot, Type type);
        ~LineChartStraightLine();

        QCPItemText* GetLabel() const {
            return m_axisLabel.label;
        }

        QCPItemLine* GetArrow() const {
            return m_axisLabel.arrow;
        }

        double GetValue() const {
            if (m_type == XAxis) {
                return this->point1->coords().x();
            }
            else if (m_type == YAxis) {
                return this->point1->coords().y();
            }
        }

        void SetValue(double value) {
            if (m_type == XAxis) {
                XAxisLineUpdate(value);
            }
            else if (m_type == YAxis) {
                YAxisLineUpdate(value);
            }
        }

        void XAxisLineUpdate(double x_value);
        void YAxisLineUpdate(double y_value);

        void updataStyle2DGuideLineProperty();

        void setLineChartStraightLineID(int lineID) { m_straightLineID = lineID; };
        int getLineChartStraightLineID() const { return m_straightLineID; };

        void setStyle2DGuideLineProperty(Style2D_GuideLineProperty* guideLineProperty) { m_guideLineProperty  = guideLineProperty; };
        Style2D_GuideLineProperty* getStyle2DGuideLineProperty() {return m_guideLineProperty;};

        /// @brief 文件写出至工程文件
        QDomElement& writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso = false);

        /// @brief 从工程文件读入数据
        void readFromProjectFile(QDomElement* element, bool isdiso = false);

    public slots:
        void slot_selectChange(bool selected);
        void slot_update();

    protected:
        void mousePressEvent(QMouseEvent* event, const QVariant& details) override;
        void mouseMoveEvent(QMouseEvent* event, const QPointF& startPos) override;

    private:
        void XLineLabelUpdate(double x_value);
        void YLineLabelUpdate(double y_value);

        Type m_type;
        int m_straightLineID{-1};
        Style2D_GuideLineProperty* m_guideLineProperty;
        IntersectionDataLabel m_axisLabel;
        QList<QPair<QCPGraph*, IntersectionDataLabel>> m_dataLabelList;
    };
}
#endif // PST_LINECHARTSTRAIGHTLINE_H
